package com.vtech.mybatis.baseservice.plugins;

import com.vtech.mybatis.baseservice.util.PluginUtil;
import org.mybatis.generator.api.GeneratedJavaFile;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.Interface;
import org.mybatis.generator.api.dom.java.JavaVisibility;
import org.mybatis.generator.internal.util.StringUtility;

import java.util.ArrayList;
import java.util.List;

import static org.mybatis.generator.internal.util.StringUtility.stringHasValue;
import static org.mybatis.generator.internal.util.messages.Messages.getString;

/**
 * TODO
 * 生成service接口和serviceImpl类
 *
 * @author houzhiwei
 * @Date 2015年12月14日 上午11:18:29
 */
public class ServicePlugin extends PluginAdapter
{
    //检测是否有需要的配置参数，没有则不执行插件
    //MapperConfigPlugin
    public boolean validate(List<String> warnings)
    {
        boolean valid = true;

        if (!stringHasValue(properties
                                    .getProperty("targetProject"))) { //$NON-NLS-1$
            warnings.add(getString("ValidationError.18", //$NON-NLS-1$
                                   "MapperConfigPlugin", //$NON-NLS-1$
                                   "targetProject")); //$NON-NLS-1$
            valid = false;
        }

        if (!stringHasValue(properties
                                    .getProperty("targetPackage"))) { //$NON-NLS-1$
            warnings.add(getString("ValidationError.18", //$NON-NLS-1$
                                   "MapperConfigPlugin", //$NON-NLS-1$
                                   "targetPackage")); //$NON-NLS-1$
            valid = false;
        }
        /*if (!stringHasValue(properties
                                    .getProperty("rootServiceInterface"))) { //$NON-NLS-1$
            warnings.add(getString("ValidationError.18", //$NON-NLS-1$
                                   "MapperConfigPlugin", //$NON-NLS-1$
                                   "rootServiceInterface")); //$NON-NLS-1$
            valid = false;
        }*/

        return valid;
    }

    /**
     * 生成service接口
     */
    @Override
    public List<GeneratedJavaFile> contextGenerateAdditionalJavaFiles(IntrospectedTable introspectedTable)
    {
        // 主键类型
        int keySize = introspectedTable.getPrimaryKeyColumns().size();
        if (keySize > 1) {
            return null;
        }
        // 获取实体类
        FullyQualifiedJavaType entityType = new FullyQualifiedJavaType(introspectedTable.getBaseRecordType());

        String rootInterface = properties.getProperty("rootServiceInterface");
        if (!StringUtility.stringHasValue(rootInterface)) {
            rootInterface = "com.vtech.mybatis.baseservice.base.BaseService";
        }
        String targetPackage = properties.getProperty("targetPackage");
        // 接口全名
        FullyQualifiedJavaType serviceType = new FullyQualifiedJavaType(targetPackage + "." + entityType.getShortName() + "Service");
        Interface interfaze = new Interface(serviceType);
        interfaze.setVisibility(JavaVisibility.PUBLIC);
        // extends
        if (keySize == 0)
            rootInterface = PluginUtil.insertStr(rootInterface.lastIndexOf('.') + 1, "NK", rootInterface);
        FullyQualifiedJavaType rootInterfaceType = new FullyQualifiedJavaType(rootInterface);
        interfaze.addImportedType(rootInterfaceType);

        // import接口
        StringBuilder superInterface = new StringBuilder();
        superInterface.append(rootInterfaceType.getShortName());
        superInterface.append("<");
        superInterface.append(entityType.getShortName());
        if (keySize == 1) {
            FullyQualifiedJavaType primaryKeyType = new FullyQualifiedJavaType(introspectedTable.getPrimaryKeyColumns().get(0).getFullyQualifiedJavaType().getShortName());
            interfaze.addImportedType(primaryKeyType);
            superInterface.append(",");
            superInterface.append(primaryKeyType.getShortName());
        }
        superInterface.append(">");
        interfaze.addSuperInterface(new FullyQualifiedJavaType(superInterface.toString()));

        // import实体类
        interfaze.addImportedType(entityType);
        GeneratedJavaFile serviceFiles = new GeneratedJavaFile(interfaze, properties.getProperty("targetProject"), context.getJavaFormatter());
        List<GeneratedJavaFile> files = new ArrayList<GeneratedJavaFile>(1);
        files.add(serviceFiles);
        return files;
    }
}
